/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.partaljevic.tapestry.gambling.pages;

import com.partaljevic.gambling.beans.Match;
import com.partaljevic.gambling.Customer;
import com.partaljevic.gambling.Identifiable;
import com.partaljevic.gambling.beans.Pair;
import com.partaljevic.tapestry.gambling.service.Customers;
import com.partaljevic.tapestry.gambling.service.MatchesService;
import com.partaljevic.tapestry.gambling.service.PairsOfTickets;
import com.partaljevic.tapestry.gambling.service.TicketService;
import java.util.LinkedList;
import java.util.List;
import org.apache.tapestry5.ValidationTracker;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

/**
 *
 * @author Danilo
 */
public class Ticket {

    @Inject
    private Customers customers;
    @InjectPage
    private Welcome welcome;
    @Property
    private List<Pair> pairOfTicket = new LinkedList<Pair>();
    @Property
    private int rowIndex;
    @Property
    private String billing;
    @Property
    @Persist
    private Pair pair;
    @Property
    private Customer currentCustomer;
    @Environmental
    private ValidationTracker tracker;
    @Property
    @Inject
    private MatchesService matchesService;
    @Property
    private Match match;
    @Inject
    private TicketService tickets;
    @Inject
    private PairsOfTickets pairs;

    void onActivate(long id) {
        currentCustomer = customers.retrieve(id);
    }

    Match findMatchById(long id) {
        return matchesService.retreive(++id);
    }

    @OnEvent(component = "button1", value = "selected")
    public void button1(int index) {
        Pair p = new Pair(findMatchById(index), "1");
        p.setId((long)pairs.retreive().size()+1);
        if (pairOfTicket.isEmpty()) {
            pairOfTicket.add(p);
        } else if (matchExist(p)) {
            pairOfTicket.add(p);
        }

    }

    @OnEvent(component = "button2", value = "selected")
    public void button2(int index) {

       Pair p = new Pair(findMatchById(index), "X");
       p.setId((long)pairs.retreive().size()+1);
        if (pairOfTicket.isEmpty()) {
            pairOfTicket.add(p);
        } else if (matchExist(p)) {
            pairOfTicket.add(p);
        }

    }

    @OnEvent(component = "button3", value = "selected")
    public void button3(int index) {
        Pair p = new Pair(findMatchById(index), "2");
        p.setId((long)pairs.retreive().size()+1);
        if (pairOfTicket.isEmpty()) {
            pairOfTicket.add(p);
        } else if (matchExist(p)) {
            pairOfTicket.add(p);
        }
    }

    public Object onSubmitFromOkForm() {
        double bil = 0;
        try {
            bil = Double.parseDouble(billing);
        } catch (Exception e) {
            tracker.recordError("You have to enter number in field for billing");
        }
        if (pairOfTicket.size() == 0) {
            tracker.recordError("You have to choose at least one match to create ticket");
        } else if (bil <= 0) {
            tracker.recordError("Error. Field for billing must have value higher than 0.");
        } else if (bil > currentCustomer.getScore()) {
            tracker.recordError("Error. Not enough score on your account.");
        } else {
            com.partaljevic.gambling.beans.Ticket t = new com.partaljevic.gambling.beans.Ticket(pairOfTicket, bil);
            t.setCustomer(currentCustomer);

            currentCustomer.addTicket(tickets.save(t));
            for (Pair match1 : pairOfTicket) {
                match1.setTicket(t);
                pairs.save(match1);
            }
            currentCustomer.setScore(currentCustomer.getScore() - bil);
            customers.update(currentCustomer);
            pairOfTicket.clear();
            welcome.setID(Identifiable.class.cast(currentCustomer).getID());
            return welcome;
        }
        return null;
    }

    public Object onSubmitFromCancelForm() {
        if (!pairOfTicket.isEmpty()) {
            pairOfTicket.remove(pairOfTicket.size()-1);
            return null;
        } else {
            welcome.setID(Identifiable.class.cast(currentCustomer).getID());
            return welcome;
        }
    }

    public int getRowIndexP1() {
        return rowIndex + 1;
    }

    boolean matchExist(Pair match) {
        for (Pair matchh : pairOfTicket) {
            if (matchh.getHome().equalsIgnoreCase(match.getHome())) {
                pairOfTicket.remove(matchh);
                pairOfTicket.add(match);
                return false;
            }
        }
        return true;
    }

    public long getID() {
        return Identifiable.class.cast(currentCustomer).getID();
    }

    public long onPassivate() {
        return Identifiable.class.cast(currentCustomer).getID();
    }
}
